package com.wushi.lenovo.asia5b.app_di.profile.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.model.ResultModel;
import com.mvp.helper.WCallBack;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.NewAddressActivityView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.CacheType;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;
import com.wushi.lenovo.asia5b.app_model.profile.CountryModel;
import com.wushi.lenovo.asia5b.app_model.profile.PostCodeModel;
import com.wushi.lenovo.asia5b.app_model.profile.ProvinceOrCityModel;
import com.wushi.lenovo.asia5b.app_utils.helper.CacheHelper;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function4;


public class NewAddressActivityPresenter extends BasePresenter<NewAddressActivityView> {

    ApiService apiService;

    public static class CountrysStateCityPostResModel extends ResultModel {
        public List<CountryModel> countrys;
        public List<ProvinceOrCityModel> provinces;
        public List<ProvinceOrCityModel> cityes;
        public PostCodeModel postCodeByCity;
    }

    @Inject
    public NewAddressActivityPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void getCountrysStateCityPost(AddressModel model) {
        Observable<ResultModel<List<CountryModel>>> countrys = apiService.getCountrys();
        Observable<ResultModel<List<ProvinceOrCityModel>>> provinces = apiService.getProvincesById(model.getCountry_id());
        Observable<ResultModel<List<ProvinceOrCityModel>>> cityes = apiService.getCityesById(model.getState_id());
        Observable<ResultModel<PostCodeModel>> postCodeByCity = apiService.getPostCodeByCity(model.getCity_id());
        Observable<CountrysStateCityPostResModel> zip = Observable.zip(countrys, provinces, cityes, postCodeByCity, new Function4<ResultModel<List<CountryModel>>, ResultModel<List<ProvinceOrCityModel>>, ResultModel<List<ProvinceOrCityModel>>, ResultModel<PostCodeModel>, CountrysStateCityPostResModel>() {
            @Override
            public CountrysStateCityPostResModel apply(ResultModel<List<CountryModel>> listResultModel, ResultModel<List<ProvinceOrCityModel>> listResultModel2, ResultModel<List<ProvinceOrCityModel>> listResultModel3, ResultModel<PostCodeModel> postCodeModelResultModel) throws Exception {
                CountrysStateCityPostResModel resModel = new CountrysStateCityPostResModel();
                resModel.setResult(0);
                if (listResultModel.isSuccess() && listResultModel2.isSuccess() && listResultModel3.isSuccess() && postCodeModelResultModel.isSuccess()) {
                    resModel.countrys = listResultModel.getData();
                    resModel.provinces = listResultModel2.getData();
                    resModel.cityes = listResultModel3.getData();
                    resModel.postCodeByCity = postCodeModelResultModel.getData();
                    resModel.setResult(1);
                } else if (!listResultModel.isSuccess()) {
                    listResultModel.setMsg(listResultModel.getMsg());
                } else if (!listResultModel2.isSuccess()) {
                    listResultModel.setMsg(listResultModel2.getMsg());
                } else if (!listResultModel3.isSuccess()) {
                    listResultModel.setMsg(listResultModel3.getMsg());
                } else if (!postCodeModelResultModel.isSuccess()) {
                    listResultModel.setMsg(postCodeModelResultModel.getMsg());
                }
                return resModel;
            }
        });
        baseSubscribe(zip,resModel -> {
            if(resModel.isSuccess()) {
                getView().getCountrysStateCityPostResult(resModel.countrys,resModel.provinces,resModel.cityes,resModel.postCodeByCity);
            }
        });
    }

    public void getCountrys() {
        CacheHelper.getCountrys(new WCallBack<List<CountryModel>>() {
            @Override
            public void callback(List<CountryModel> countryModels) {
                getView().getCountrysResult(countryModels);
            }
        }, CacheType.LOCAL);
//        baseSubscribe(apiService.getCountrys(), listResultModel -> {
//            getView().getCountrysResult(listResultModel.getData());
//        });
    }

    /**
     * @param country_id
     */
    public void getProvincesById(int country_id) {

        baseSubscribe(
                apiService.getProvincesById(country_id)
                , listResultModel -> {
                    getView().getProvincesResult(listResultModel);

                });
    }

    /**
     * @param state_id
     */
    public void getCityesById(int state_id) {
        baseSubscribe(apiService.getCityesById(state_id)
                , listResultModel -> {
                    getView().getCityesResult(listResultModel);
                });
    }

    public void addAddress(Map<String, Object> map, boolean isShipAddressDefault, boolean isBillAddressDefault) {
        baseSubscribe(apiService.addAddress(map)
                , resultModel -> {
                    getView().addAddressResult(resultModel, isShipAddressDefault, isBillAddressDefault);
                });
    }

    public void getPostCodeByCity(int city_id) {
        baseSubscribe(apiService.getPostCodeByCity(city_id)
                , resultModel -> {
                    getView().getPostCodeByCityResult(resultModel);
                });
    }

    public void updateAddress(AddressModel model, Map<String, Object> map, boolean isShipAddressDefault, boolean isBillAddressDefault) {
        Observable<ResultModel> updateAddress = apiService.updateAddress(map);


        baseSubscribe(updateAddress/*.flatMap(new Function<ResultModel, ObservableSource<ResultModel>>() {
                    @Override
                    public ObservableSource<ResultModel> apply(ResultModel resultModel) throws Exception {
                        if (resultModel.isSuccess()) {
                            if (!model.isShipAddressDefault() && isShipAddressDefault) {//设置默认地址
                                return apiService.setDefaultAddress(model.getId(), UserInfo.getUserInfo().getId(), "ship_address_id");
                            }
                        }
                        return Observable.just(resultModel);
                    }
                }).flatMap(new Function<ResultModel, ObservableSource<ResultModel>>() {
                    @Override
                    public ObservableSource<ResultModel> apply(ResultModel resultModel) throws Exception {
                        if (resultModel.isSuccess()) {
                            if (!model.isBillAddressDefault() && isBillAddressDefault) {//设置默认地址
                                return apiService.setDefaultAddress(model.getId(), UserInfo.getUserInfo().getId(), "bill_address_id");
                            }
                        }
                        return Observable.just(resultModel);
                    }
                })*/
                , resultModel -> {
                    getView().updateAddressResult(resultModel, isShipAddressDefault, isBillAddressDefault);
                });
    }

}